using System;
using GameFrameworkLibraly.BaseObjects;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace GameFrameworkLibraly.Controls
{
    public class LabelControl:Control
    {
        /// <summary>
        /// Text that will be printed
        /// </summary>
        public string Text { get; set; }

        /// <summary>
        /// Font that will be used to print Text
        /// </summary>
        public SpriteFont SpriteFont { get; set; }

        private Vector2 _textPosition;

        /// <summary>
        /// Creates a new instance of LabelControl
        /// </summary>
        /// <param name="game">Current game</param>
        public LabelControl(MyGame game):base(game)
        {
        }

        /// <summary>
        /// Creates a new instance of LabelControl
        /// </summary>
        /// <param name="game">Current game</param>
        /// <param name="text">Text that will be printed</param>
        /// <param name="spriteFont">Font that will be used to print Text</param>
        /// <param name="destinationRectangle">Defines rectangle where the control must be drawn</param>
        public LabelControl(MyGame game, String text, SpriteFont spriteFont, Rectangle destinationRectangle) : this(game)
        {
            Text = text;
            SpriteFont = spriteFont;
            DestinationRectangle = destinationRectangle;
        }

        #region Control

        /// <summary>
        /// Draws the control
        /// </summary>
        /// <param name="spriteBatch"></param>
        public override void Draw(SpriteBatch spriteBatch)
        {
            if(!Visible)
                return;
            if(BackgroundTexture!=null)
                spriteBatch.Draw(BackgroundTexture,DestinationRectangle,BackColorFilter);
            if(!string.IsNullOrEmpty(Text) && SpriteFont!=null)
                spriteBatch.DrawString(SpriteFont,Text,_textPosition,ForeColor);
        }

        /// <summary>
        /// Updates state of the control
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Update(GameTime gameTime)
        {
            if (!Enabled)
                return;
            if (SpriteFont != null && !string.IsNullOrEmpty(Text))
            {
                Vector2 textSize = SpriteFont.MeasureString(Text);
                _textPosition = new Vector2((int)(DestinationRectangle.Left + (DestinationRectangle.Width - textSize.X) / 2),
                                            (int)(DestinationRectangle.Top + (DestinationRectangle.Height - textSize.Y) / 2));
            }
        }
        
        #endregion
    }
}
